package com.trainoo.payment.mock.config;

import java.text.MessageFormat;
import java.util.HashMap;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2023/6/7 17:19:00
 */
public class CallbackUrl {

    private static final HashMap<String, String> callbackUrlMap = new HashMap<>();

    static {
        callbackUrlMap.put("default",    "http://{0}:9802/payNotify/mock"); // 默认配置
        callbackUrlMap.put("10.9.0.119", "http://10.9.0.119:8730/payNotify/mock"); // 重庆资金测试
        callbackUrlMap.put("10.13.1.81", "http://10.13.1.81:9802/mock/payNotify"); // 山西渠道测试
        callbackUrlMap.put("10.9.2.55",  "http://10.9.2.55:9802/payNotify/mock/default"); // 河南渠道测试
    }

    public static String getDefault() {
        return callbackUrlMap.get("default");
    }

    public static HashMap<String, String> getUrlMap() {
        return callbackUrlMap;
    }

    public static HashMap<String, String> setUrl(String ip, String url) {
        callbackUrlMap.put(ip, url);
        return callbackUrlMap;
    }

    public static HashMap<String, String> delUrl(String ip) {
        callbackUrlMap.remove(ip);
        return callbackUrlMap;
    }

    public static String getUrl(String ip, String callbackUrl) {
        // 判断是否传入了callbackUrl, 传了直接返回
        if (callbackUrl != null && callbackUrl.trim().length() > 0) {
            return callbackUrl;
        }
        // 取配置中的回调地址
        String url = callbackUrlMap.get(ip);
        if (url != null) {
            return url;
        }
        // 如果配置中没有，则返回默认地址
        return MessageFormat.format(getDefault(), ip);
    }
}
